export function createScene() {
  var scene = new THREE.Scene();
  // 添加灯光
  let light = new THREE.HemisphereLight(0xffffff);
  light.position.set(0, 40, 0);
  scene.add(light);

  light = new THREE.DirectionalLight(0xffffff);
  light.position.set(0, 40, -10);
  scene.add(light);
  // 全景场景
  const geometry = new THREE.SphereGeometry(500, 60, 60);
  // 按z轴翻转
  geometry.scale(1, 1, -1);
  // 添加贴图

  const mesh = new THREE.Mesh(geometry);

  scene.add(mesh);
  return {
    scene,
    mesh,
  };
}

// 加载交互点
export function loadMarker(interactPoints, { scene }) {
  // 添加问号标记点
  var pointTexture = new THREE.TextureLoader().load(
    "./assets/images/point.png"
  );

  const interactMeshes = [];
  var pointMaterial = new THREE.SpriteMaterial({ map: pointTexture });
  interactPoints.map((item) => {
    let point = new THREE.Sprite(pointMaterial);
    point.name = item.name;
    point.scale.set(item.scale * 1.2, item.scale * 1.2, item.scale * 1.2);
    point.position.set(item.x, item.y, item.z);

    point.onClick = function () {
      item.onClick?.(item);
    };
    interactMeshes.push(point);
    scene.add(point);
  });
  // 加载地标模型
  const anchorMeshes = [];
  var loader = new THREE.GLTFLoader();
  loader.load("./assets/models/anchor.gltf", function (object) {
    object.scene.traverse((child) => {
      if (child.isMesh) {
        child.castShadow = true;
        child.receiveShadow = true;
        child.material.metalness = 0.4;
        if (child.name.includes("黄")) {
          // child.material.color = new THREE.Color(0xfffc00);
          child.material.color = new THREE.Color(0xff0c00);
        } else {
          child.material.color = new THREE.Color(0x0000f0);
        }
      }
    });
    // object.scene.rotation.y = (Math.PI / 2) * 3;
    interactPoints.map((item) => {
      let anchor = object.scene.clone();
      anchor.position.set(item.x, item.y + 3, item.z);
      anchor.name = item.name;
      anchor.scale.set(item.scale * 3, item.scale * 3, item.scale * 3);
      anchorMeshes.push(anchor);
      scene.add(anchor);
    });
  });

  return { interactMeshes, anchorMeshes };
}
// 创建文字
export function makeTextSprite(message, parameters) {
  if (parameters === undefined) parameters = {};
  var fontface = parameters.hasOwnProperty("fontface")
    ? parameters["fontface"]
    : "Arial";
  var fontsize = parameters.hasOwnProperty("fontsize")
    ? parameters["fontsize"]
    : 32;
  var borderThickness = parameters.hasOwnProperty("borderThickness")
    ? parameters["borderThickness"]
    : 40;
  var borderColor = parameters.hasOwnProperty("strokeStyle")
    ? parameters["strokeStyle"]
    : "red";
  var canvas = document.createElement("canvas");
  const rate = 2;
  canvas.width = 256 * rate;
  canvas.height = 256 * rate;
  var context = canvas.getContext("2d");
  context.font = fontsize + "px " + fontface;
  // var metrics = context.measureText(message);
  // console.log(metrics)
  context.strokeStyle = borderColor;
  context.fillStyle = "red";

  // context.fillRect(0, 0, canvas.width, canvas.height);
  context.lineWidth = borderThickness;
  context.fillStyle = "#fffc00";
  context.font = 48 + "px " + fontface;
  context.fillText(
    message,
    borderThickness,
    fontsize + borderThickness,
    256 * rate
  );
  var texture = new THREE.Texture(canvas);
  texture.needsUpdate = true;

  var spriteMaterial = new THREE.SpriteMaterial({ map: texture });
  var sprite = new THREE.Sprite(spriteMaterial);
  return sprite;
}

// 加载嫌疑人
export function loadMurderer(scene) {
  var texture = new THREE.TextureLoader().load("./assets/images/murderer.png");
  var material = new THREE.SpriteMaterial({ map: texture });
  const murderer = new THREE.Sprite(material);
  murderer.name = "murderer";
  const rate = 40;
  murderer.scale.set(rate, rate, rate);
  murderer.position.set(43, -1.5, -20);
  scene.add(murderer);
}

export function Toast(message, time, scene, getPosition) {
  let { lon, lat } = getPosition();

  lat = Math.max(-85, Math.min(85, lat));
  const phi = THREE.Math.degToRad(90 - lat);
  const theta = THREE.Math.degToRad(lon);

  const x = 20 * Math.sin(phi) * Math.cos(theta);
  const y = 20 * Math.cos(phi);
  const z = 20 * Math.sin(phi) * Math.sin(theta);

  const tip = makeTextSprite(message);
  tip.scale.set(10, 10, 10);
  tip.position.set(x, y, z);
  scene.add(tip);

  setTimeout(() => {
    scene.remove(tip);
  }, parseInt(time, 10) || 2000);
}

export function findInRoom(scene) {
  // 创建一个canvas绘制文字
  let outsideTextTip = makeTextSprite("进入室内查找");
  outsideTextTip.scale.set(2.2, 2.2, 2);
  outsideTextTip.position.set(-0.35, -1, 10);
  scene.add(outsideTextTip);
  return outsideTextTip;
}

export function loadMeterial(mesh, key) {
  mesh.material = new THREE.MeshBasicMaterial({
    map: new THREE.TextureLoader().load(`./assets/images/${key}_low.jpg`),
  });
  //异步加载高清纹理图
  new THREE.TextureLoader().load(`./assets/images/${key}.jpg`, (texture) => {
    mesh.material = new THREE.MeshBasicMaterial({ map: texture });
  });
}

export function rotationY(anchorMeshes) {
  anchorMeshes.map((item) => {
    item.rotation.y += 0.2;
  });
}

export function rotationX(anchorMeshes) {
  anchorMeshes.map((item) => {
    item.scale.set(2, 2, 2);
    // item.rotation.z += 0.2;
  });
}
